package utils

import (
	"crypto/md5"
	"crypto/sha256"
	"encoding/json"
	"fmt"
	"io"
	"log"
)

func GenSaltPassword(salt, password string) string {
	s1 := sha256.New()
	s1.Write([]byte(password))
	str1 := fmt.Sprintf("%x", s1.Sum(nil))
	s2 := sha256.New()
	s2.Write([]byte(str1 + salt))
	return fmt.Sprintf("%x", s2.Sum(nil))
}

// MD5 md5加密
func MD5(s string) string {
	h := md5.New()
	_, err := io.WriteString(h, s)
	if err != nil {
		log.Println("MD5 write error:", err)
		return ""
	}
	return fmt.Sprintf("%x", h.Sum(nil))
}

func Obj2Json(s interface{}) string {
	bts, _ := json.Marshal(s)
	return string(bts)
}

func StrInSlice(slice []string, str string) bool {
	for _, v := range slice {
		if str == v {
			return true
		}
	}
	return false
}
func StrInBlackList(slice []string, str string) bool {
	for _, v := range slice {
		if str == v {
			return true
		}
	}
	return false
}
func StrInWhileList(slice []string, str string) bool {
	for _, v := range slice {
		if v == "*" { // 所有地址都放行
			return true
		}
		if str == v {
			return true
		}
	}
	return false
}
